package ldh.im.fxgui.util;

import javafx.stage.Popup;
import ldh.im.fxbase.component.EmojiPane;
import ldh.im.fxbase.component.ScreenCapturePane;
import ldh.im.fxbase.data.pojo.ImSession;
import ldh.im.fxbase.data.pojo.ImUser;
import ldh.im.fxgui.component.message.MessageListViewHandler;
import ldh.im.fxgui.controller.MainController;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ImSingleton {

    private static ImSingleton INSTANCE = new ImSingleton();
    private ImSingleton() {}

    private volatile EmojiPane emojiPane = null;
    private volatile ScreenCapturePane screenCapturePane = null;
    private volatile MainController mainController = null;
    private volatile Popup MAIN_POPUP = null;
    private volatile MessageListViewHandler sendMessageHandler = null;
    private Map<String, Object> cacheMap = new ConcurrentHashMap<>();

    private volatile ImUser loginImUser = null;

    public ImUser getLoginImUser() {
        return loginImUser;
    }

    public void setLoginImUser(ImUser loginImUser) {
        this.loginImUser = loginImUser;
    }

    public static ImSingleton getInstance() {
        return INSTANCE;
    }

    public MessageListViewHandler getSendMessageHandler() {
        return sendMessageHandler;
    }

    public void setSendMessageHandler(MessageListViewHandler sendMessageHandler) {
        this.sendMessageHandler = sendMessageHandler;
    }

    public void putData(String key, Object obj) {
        this.cacheMap.put(key, obj);
    }

    public <T> T getData(String key) {
        return (T) this.cacheMap.get(key);
    }

    public void setMainController(MainController mainController) {
        this.mainController = mainController;
    }

    public MainController getMainController() {
        return mainController;
    }

    public void switchMessageSession(ImSession imSession) {
        mainController.switchMessageSession(imSession, null);
    }

    public EmojiPane getEmojiPane() {
//        if (emojiPane == null) {
//            synchronized (this) {
//                if (emojiPane == null) {
//                    emojiPane = new EmojiPane();
//                }
//            }
//        }
        return new EmojiPane();
    }

    public ScreenCapturePane getScreenCapturePane() {
        if (screenCapturePane == null) {
            synchronized (UiUtil.class) {
                if (screenCapturePane == null) {
                    screenCapturePane = new ScreenCapturePane();
                }
            }
        }
        return screenCapturePane;
    }

    public Popup getMainPopup() {
        if (MAIN_POPUP == null) {
            MAIN_POPUP = new Popup();
            MAIN_POPUP.setAutoHide(true);
        }
        return MAIN_POPUP;
    }
}
